﻿@model Netnr.Domain.DocSet
@{
    ViewData["Title"] = ViewContext.RouteData.Values["action"].ToString() + " Doc";
    var uinfo = new UserAuthAid(ViewContext.HttpContext).Get();
}
<style>
    .adf {
        margin: auto;
        max-width: 400px;
    }

        .adf textarea {
            height: 120px;
        }
</style>
<div class="container">
    <div class="row">
        <div class="col-12">
            <form class="adf my-5" onsubmit="return savedoc()">
                <div class="form-group">
                    <input type="text" class="form-control" name="DsName" value="@Model?.DsName" placeholder="名称" maxlength="50" autofocus />
                    <small class="form-text text-muted">必填</small>
                </div>
                <div class="form-group">
                    <textarea class="form-control" name="DsRemark" placeholder="描述" maxlength="200">@Model?.DsRemark</textarea>
                </div>
                <div class="form-group">
                    <select class="custom-select" name="DsOpen" data-value="@(Model?.DsOpen??1)">
                        <option value="1">公开（只读）</option>
                        <option value="0">私有</option>
                    </select>
                </div>
                <div class="form-group">
                    <input type="number" class="form-control" name="Spare1" value="@Model?.Spare1" placeholder="分享码，不需要分享可不填写" maxlength="4" />
                    <small class="form-text text-muted">私有时，可根据分享码 <b>查看</b></small>
                </div>
                <input type="hidden" name="DsCode" value="@Model?.DsCode" />
                <input type="hidden" id="hid_uid" name="Uid" value="@uinfo.UserId" />
                <button type="submit" class="btn btn-primary">
                    <i class="fa fa-save"></i>&nbsp;保存
                </button>
            </form>
        </div>
    </div>
</div>
<script>
    var open = $('select[name="DsOpen"]'), sc = $('input[name="Spare1"]');
    open.val(open.attr('data-value'));

    function savedoc() {

        var err = [];
        if ($('input[name="DsName"]').val().trim() == "") {
            err.push('名称 必填');
        }
        if (sc.val() != "" && sc.val().length != 4) {
            err.push('分享码为 4 位数字');
        }

        if (err.length) {
            jz.alert(err.join('<br/>'));
        } else {
            $.ajax({
                url: "/doc/item/SaveDocSet",
                type: 'post',
                dataType: 'json',
                data: $('form').serialize(),
                success: function (data) {
                    if (data.code == 200) {
                        location.href = "/doc/user/" + $('#hid_uid').val();
                    } else {
                        alert(data.msg);
                    }
                }
            })
        }

        return false;
    }

    //分享码
    sc[0].readOnly = open.val() == 1;
    open.change(function () {
        sc[0].readOnly = open.val() == 1;
    });
    sc.on('input', function () {
        if (this.value.length > 4) {
            this.value = String(this.value).substr(0, 4);
        }
    });
</script>